package m2;

import com.rabbitmq.client.*;
import com.rabbitmq.client.impl.AMQImpl;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
         ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
         final Channel c = con.createChannel();
        //Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'hello-world' in vhost '/': received 'true' but current is 'false', class-id=50, method-id=10)
        //出现这个错更换新队列
        c.queueDeclare("hello-world-new hello",true,false,false,null);

        DeliverCallback deliverCallback = new DeliverCallback() {
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] body = delivery.getBody();
                String s1 = new String(body);
                for(int i = 0 ; i<s1.length() ; i++ ){
                    if(s1.charAt(i)== '.'){
                        try {
                            Thread.sleep(1000);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println(s1);
             //c.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
                c.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            public void handle(String s) throws IOException {

            }
        };
        c.basicQos(1);/*每次只接受一条消息，未处理完之前不处理下一条数据*/
    c.basicConsume("hello-world-new hello",true,deliverCallback,cancelCallback);

    }
        // 连接
//        ConnectionFactory f = new ConnectionFactory();
//        f.setHost("192.168.64.140");   // wht6.cn
//        f.setPort(5672);
//        f.setUsername("admin");
//        f.setPassword("admin");
//        Connection con = f.newConnection();
//        Channel c = con.createChannel();// 通信的通道
//
//        c.queueDeclare("task_queue",true,false,false,null);
//
//        // 回调对象
//        DeliverCallback deliverCallback = (consumerTag, message) -> {
//            String s = new String(message.getBody());
//            System.out.println(s);
//            // 遍历所有字符，遇到 '.' 暂停1秒
//            for (int i = 0; i < s.length(); i++) {
//                if (s.charAt(i) == '.') {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                    }
//                }
//            }
//            // c.basicAck(回执, 是否同时确认收到过的所有消息);
//            c.basicAck(message.getEnvelope().getDeliveryTag(), false);
//            System.out.println("-------------------------------消息处理完成");
//        };
//        CancelCallback cancelCallback = consumerTag -> {};
//        // 每次只收一条，处理完之前不收下一条
//        c.basicQos(1);
//
//        // 接收消息
//        // 第二个参数用 false, 手动确认，手动发送回执
//        c.basicConsume("task_queue",false,deliverCallback,cancelCallback);
    }
